//
// Created by denglibin on 2021/6/1.
//
#include <window.h>
#include "plane.h"
#include "fun.h"
#define WIN_W 600
#define WIN_H 800
static MyPlane *myPlane;
static double scale = 0.2;
static int speed = 10;
static SDL_Texture *bullet_texture;
static int bullet_counter = 0;
static int bullet_y = 0;
static int bullet_x = 0;
/**
 * 初始化
 */
static void init(Window * window){
    int w, h;
    SDL_Texture * texture = createTexture("D:\\CLionProjects\\sdl2_demo\\resources\\my_plane.png",
                                          window->winRender);
    SDL_QueryTexture(texture, NULL, NULL, &w, &h);
    myPlane = MyPlaneCreate(texture, w, h,
                              WIN_W/2 - (int )(w * scale) /2,
                            WIN_H - h * scale);

    bullet_texture = createTexture("D:\\CLionProjects\\sdl2_demo\\resources\\bullet.png",
                                   window->winRender);
    bullet_y = myPlane->basePlane.y -10;
    bullet_x = (int)(myPlane->basePlane.x + myPlane->basePlane.w * scale /2 - 10);
}

/**
 * 绘制我的飞机
 * @param window
 */
static void drawMyPlane(Window* window){
    SDL_Rect srcRect = {0, 0, myPlane->basePlane.w, myPlane->basePlane.h};
    SDL_Rect distRect = {myPlane->basePlane.x, myPlane->basePlane.y, (int)(srcRect.w * scale), (int)(srcRect.h * scale) };
    SDL_RenderCopy(window->winRender, myPlane->basePlane.texture, &srcRect, &distRect);

}

/**
 * 子弹
 * @param window
 */
static void drawBullet(Window* window){
    bullet_y -= 4;
    if(bullet_y < 0){
        return;
    }
    SDL_Rect bulletSrcRect = {0, 0, 200, 200};
    SDL_Rect bulletDistRect = { bullet_x, bullet_y, 20, 20 };
    SDL_RenderCopy(window->winRender, bullet_texture, &bulletSrcRect, &bulletDistRect);
    bullet_counter++;

}

/**
 * 绘制函数
 * @param window
 * @param event
 */
static void draw(Window* window, SDL_Event* event){

    //设置渲染器颜色
    SDL_SetRenderDrawColor(window->winRender, 0, 0, 0, 0);
    SDL_RenderClear(window->winRender); //用设置的颜色清除
    SDL_SetRenderDrawBlendMode(window->winRender, SDL_BLENDMODE_BLEND);//设置混合模式，支持alpha透明
    //我的飞机
    drawMyPlane(window);
    drawBullet(window);
    //生效
    SDL_RenderPresent(window->winRender);
    SDL_ShowWindow(window->win);
}
/**
 * 边界碰撞
 */
static int crossBorder(){
    return  myPlane->basePlane.x < 0 ||
            myPlane->basePlane.x + myPlane->basePlane.w * scale > WIN_W ||
            myPlane->basePlane.y < 0 ||
            myPlane->basePlane.y + myPlane->basePlane.h * scale > WIN_H;
}
/**
 * 事件处理
 * @param window
 * @param event
 */
void handleEvent(Window* window, SDL_Event*  event){
    if(event->type == SDL_KEYDOWN){

        switch (event->key.keysym.sym) {
            case SDLK_a:
                myPlane->basePlane.direction = LEFT;
                if(!crossBorder()){
                    myPlane->basePlane.x -= speed;
                }
                break;
            case SDLK_d:
                myPlane->basePlane.direction = RIGHT;
                if(!crossBorder()){
                    myPlane->basePlane.x += speed;
                }
                break;
            case SDLK_s:
                myPlane->basePlane.direction = DOWN;
                if(!crossBorder()){
                    myPlane->basePlane.y += speed;
                }
                break;
            case SDLK_w:
                myPlane->basePlane.direction = UP;
                if(!crossBorder()){
                    myPlane->basePlane.y -= speed;
                }
                break;
            case SDLK_SPACE:
                bullet_y = myPlane->basePlane.y -10;
                bullet_x = (int)(myPlane->basePlane.x + myPlane->basePlane.w * scale /2 - 10);
                break;

        }
    }
}

int main_plane(int argc, char** argv){
    Window * window = createWindow(WIN_W, WIN_H, "Plane");
    init(window);
    //游戏循环
    eventLoop(window, draw, handleEvent);
    WindowFree(window);

}
